#ifndef _GWBASE_BASE_CODING_H_
#define _GWBASE_BASE_CODING_H_

#include <string.h>
#include "gwbase/base/byte_order.h"

namespace dicom {

inline unsigned char GetUChar(const void* data) {
  return reinterpret_cast<const unsigned char*>(data)[0];
}

inline void PutUChar(void* data, unsigned char v) {
  reinterpret_cast<unsigned char*>(data)[0] = v;
}

inline char GetChar(const void* data) {
  return reinterpret_cast<const unsigned char*>(data)[0];
}

inline void PutChar(void* data, char v) {
  reinterpret_cast<char*>(data)[0] = v;
}

inline uint16_t GetUInt16(const void* data) {
  uint16_t be16;
  ::memcpy(&be16, data, sizeof(be16));
  return ntoh16(be16);
}

inline void PutUInt16(void* data, uint16_t v) {
  uint16_t be16 = hton16(v);
  ::memcpy(data, &be16, sizeof(be16));
}

inline uint32_t GetUInt32(const void* data) {
  uint32_t be32;
  ::memcpy(&be32, data, sizeof(be32));
  return ntoh32(be32);
}

inline void PutUInt32(void* data, uint32_t v) {
  uint32_t be32 = hton32(v);
  ::memcpy(data, &be32, sizeof(be32));
}

inline uint64_t GetUInt64(const void* data) {
  uint64_t be64;
  ::memcpy(&be64, data, sizeof(be64));
  return ntoh64(be64);
}

inline void PutUInt64(void* data, uint64_t v) {
  uint64_t be64 = htobe64(v);
  ::memcpy(data, &be64, sizeof(be64));
}
}
#endif  // _GWBASE_BASE_CODING_H_
